package core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ordnode.NodeSystem;

public abstract class Neuron implements IInputable, IOutputable {
	protected Map<IOutputable, Double> inputs;//should be a range for weight
	protected List<IInputable> outputs;
	protected Set<IOrder> outSynapse;
	protected Set<IOrder> inSynapse;
	protected NodeSystem system;
	
	public Neuron(NodeSystem system) {
		inputs = new HashMap<IOutputable, Double>();
		outputs = new ArrayList<IInputable>();
		outSynapse = new HashSet<IOrder>();
		this.system = system;
	}
	
	public void addInput(IOutputable outable) {
		if(!inputs.containsKey(outable)) {
			inputs.put(outable, (double) 0);
		}
	}
	
	public void addOutput(IInputable inputable) {
		if(!outputs.contains(inputable)) {
			outputs.add(inputable);
		}
	}
	
	public void addOrder(IOrder linkage) {
		if(linkage.start == this) {
			outSynapse.add(linkage);
			linkage.end.inSynapse.add(linkage);
		} else if(linkage.end == this) {
			inSynapse.add(linkage);
			linkage.start.outSynapse.add(linkage);
		}
	}
	
	public boolean hasOrder(Neuron toNode) {
		for(IOrder order : outSynapse) {
			if(order.end == toNode) {
				return true;
			}
		}
		return false;
	}
	
	public IOrder getOrder(Neuron toNode) {
		for(IOrder order : outSynapse) {
			if(order.end == toNode) {
				return order;
			}
		}
		return null;
	}
}
